<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('sales_add');?></h3>

<style>
    .fone_num_cls{
        width:20%;  vertical-align: top;
    }
    .fone_num_cls_timer{
        width:3%;  vertical-align: top;
    }
    #waiting_phone{color:red;font-size: 12px;vertical-align: sub;}
</style>
<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" sales-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open();?>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-success')); ?>
                <!----<a href="<?php echo site_url('auth/sales'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>-->
        </div>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php 
                //ten
                echo form_label($this->lang->line('name'), 'name');
                echo form_input(array('name' => 'name', 'id' => 'name', 'value' => '','placeholder'=>'Nhập họ và tên khách hàng vào đây'));
                echo form_error('name');
                //loại
                echo form_label($this->lang->line('sales_type'), 'type');
                echo form_dropdown('type', $this->sales_model->getListTypeCustomers(), '1');
                echo form_error('type');
                //phone
                echo form_label($this->lang->line('phone').' <span id="waiting_phone"></span>', 'phone');
                echo form_input(array('type'=>'hidden','name' => 'phone', 'id' => 'phone', 'value' => ($fone)?$fone:'')).' ';
                echo form_input(array('disabled'=>'disabled', 'name' => 'phone_fake', 'id' => 'phone_fake', 'value' => ($fone)?$fone:'','class' => 'fone_num_cls')).' ';
                //echo form_button(array('disabled'=>'disabled','id'=>'call','value' => 'call', 'name' => 'call', 'content' => $this->lang->line('clicktocall'),'class' => 'btn btn-success')).' ';
//                echo form_button(array('id'=>'request_number','value' => 'request_number', 'name' => 'request_number', 'content' => $this->lang->line('request_number'),'class' => 'btn btn-success')).' ';
                //echo form_button(array('id'=>'calculate_hours','value' => 'calculate_hours', 'name' => 'calculate_hours', 'content' => $this->lang->line('calculate_hours'),'class' => 'btn btn-success')).' ';
                echo form_button(array('id'=>'phone_busy','value' => '1', 'name' => 'phone_busy', 'content' => $this->lang->line('phone_busy'),'class' => 'btn btn btn-success')).' ';
                
                echo form_button(array('id'=>'callback','value' => '3', 'name' => 'callback', 'content' => $this->lang->line('callback'),'class' => 'btn btn-success')).' ';
                echo form_button(array('id'=>'phone_limited','value' => '2', 'name' => 'phone_limited', 'content' => $this->lang->line('phone_limited'),'class' => 'btn btn-success')).' ';
                echo form_button(array('id'=>'callok','value' => '4', 'name' => 'callok', 'content' => $this->lang->line('callok'),'class' => 'btn btn-success')).' ';
                //echo form_input(array('name' => 'phone_calltime', 'id' => 'phone_calltime', 'class'=>'fone_num_cls_timer')).'<span id="giay">giây</span>';
                echo form_error('phone');
                
                //box sms
                echo form_button(array('id' => 'sendsms', 'value' => 'sendsms', 'name' => 'sendsms', 'content' => $this->lang->line('sendsms'),'class' => 'btn btn-success sendsms'));
                
                //phone callstatus
                echo form_input(array('type'=>'hidden', 'name' => 'phone_callstatus', 'id' => 'phone_callstatus'));
                //address
                echo form_label($this->lang->line('address'), 'address');
                echo form_input(array('name' => 'address', 'id' => 'address', 'value' => '','placeholder'=>'Nhập địa chỉ khách hàng'));
                echo form_error('address');
                 //Email
                echo form_label($this->lang->line('email'), 'email');
                echo form_input(array('name' => 'email', 'id' => 'email', 'value' => '','placeholder'=>'Nhập Email khách hàng'));
                echo form_error('email');
                 //Tỉnh thành                                
                echo form_label($this->lang->line('province'), 'province');
                echo form_dropdown('province', $this->sales_model->getListProvince(), (isset($sales->province))?$sales->province:'0');
                echo form_error('province');
                //khu vực bay
                echo form_label($this->lang->line('area'), 'area');
                echo form_dropdown('area', $this->area_model->getListArea(), (isset($sales->area))?$sales->area:'');
                echo form_error('area');
                //phương tiện KM
                echo form_label($this->lang->line('communication'), 'communication');
                echo form_dropdown('communication', $this->area_model->getListCommunication(), (isset($sales->communication))?$sales->communication:'0');
                echo form_error('communication');
                //Đánh giá chất lượng cuộc gọi
                echo form_label($this->lang->line('callquality'), 'callquality');
                echo form_dropdown('callquality', $this->area_model->getListCallquality(), (isset($sales->callquality))?$sales->callquality:'0');
                echo form_error('callquality');
                //Đánh giá mất độ quan tâm của khách hàng
                echo form_label($this->lang->line('interested'), 'interested');
                echo form_dropdown('interested', $this->area_model->getListInterested(), (isset($sales->interested))?$sales->interested:'0');
                echo form_error('interested');
                //Tần suất sử dụng vé máy bay trong 1 tháng
                echo form_label($this->lang->line('frequency'), 'frequency');
                echo form_input(array('name' => 'frequency', 'id' => 'frequency', 'value' => '0'));
                echo form_error('frequency');
                //Ghi chú KH
                echo form_label($this->lang->line('note_customers'), 'note_customers');
                echo form_textarea(array('name' => 'note_customers', 'id' => 'note_customers', 'value' => '','rows'=>'5','placeholder'=>$this->lang->line('note_customers').' note vào đây'));
                echo form_error('note_customers');
                //Ghi chú Nhân viên
                echo form_label($this->lang->line('note_staffs'), 'note_staffs');
                echo form_textarea(array('name' => 'note_staffs', 'id' => 'note_staffs', 'value' => '','rows'=>'5','placeholder'=>'Ghi chú sales ghi vào đây'));
                echo form_error('note_staffs');
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-success')); ?>
                <!----<a href="<?php echo site_url('auth/sales'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>-->
        </div>
    <?php echo form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<link rel="stylesheet" href="<?php echo STATIC_URL;?>/backend/css/jquery.fancybox.css">
<script type="text/javascript" src="<?php echo STATIC_URL;?>/backend/js/jquery.fancybox.js"></script>

<script>
$(document).ready(function() {
    
    setTimeout(function () {
            $('form').submit(function() {
                    $("#simple-msg-submit").html("Vui lòng chờ... <img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/> ");
                    $(this).find("button[type='submit']").prop('disabled',true);
            });
    }, 2000);
    
    var refreshId;
    $('button[type="submit"]').attr('disabled','disabled');
    //$("#phone_calltime").hide();
    //$("#giay").hide();
    
    $( "#phone_busy" ).on('click',function() {
        document.getElementById("phone_busy").className      = "btn btn-danger";
        document.getElementById("phone_limited").className   = "btn btn-success";
        document.getElementById("callback").className        = "btn btn-success";
        document.getElementById("callok").className          = "btn btn-success";
        $('button[type="submit"]').attr('disabled','disabled');
        var phone = $("#phone").val();
        updateURL = '/auth/sales/getphone?term=phone_busy&phone='+phone;
        
        $('#phone_busy').attr('disabled','disabled');
        $('#waiting_phone').show().html('Vui lòng chờ <img src="/static/templates/frontend/home/images/loading.gif"/>');
         setTimeout(function () {
                $.ajax(
                {
                    url : updateURL,
                    cache: true,
                    beforeSend: function(){

                    },
                    success:function(data)
                    {
                        if(data == ''){
                            alert("Hết số!");
                        } else {
                            document.getElementById("phone").value              = data;
                            document.getElementById("phone_fake").value         = data;
                            
                            $('#waiting_phone').hide();
                            $('#request_number').attr('disabled','disabled');
                            $('#call').removeAttr('disabled');
                            $('#phone_busy').removeAttr('disabled');
                            $('#phone_limited').removeAttr('disabled');
                            $('#callback').removeAttr('disabled');
                        }
                    },
                    error: function() 
                    {
                        alert("Lỗi AJAX! Vui lòng liên hệ Admin!");
                    }
                });
        }, 2000);
    });
    
    $( "#phone_limited" ).on('click',function() {
        clearTimeout(refreshId);
        document.getElementById("phone_callstatus").value    = $(this).val();
        var phone       =   $("#phone").val();
        updateURL       = '/auth/sales/getphone?term=phone_limited&phone='+phone;
        $.ajax(
        {
            url : updateURL,
            success:function(data)
            {
                document.getElementById("phone_busy").className      = "btn btn-success";
                document.getElementById("phone_limited").className   = "btn btn-danger";
                document.getElementById("callback").className        = "btn btn-success";
                document.getElementById("callok").className          = "btn btn-success";

                $('#phone_busy').attr('disabled','disabled');
                $('#callback').attr('disabled','disabled');
                $('#callok').attr('disabled','disabled');
                $('#phone_limited').attr('disabled','disabled');
                $('button[type="submit"]').removeAttr('disabled');
            },
            error: function() 
            {
                alert("Lỗi AJAX! Vui lòng liên hệ Admin!");
            }
        });
    });
    
    $( "#callback" ).on('click',function() {
        clearTimeout(refreshId);
        document.getElementById("phone_callstatus").value    = $(this).val();
        var phone       =   $("#phone").val();
        updateURL       = '/auth/sales/getphone?term=callback&phone='+phone;
        $.ajax(
        {
            url : updateURL,
            success:function(data)
            {
                document.getElementById("phone_busy").className      = "btn btn-success";
                document.getElementById("phone_limited").className   = "btn btn-success";
                document.getElementById("callback").className        = "btn btn-danger";
                document.getElementById("callok").className          = "btn btn-success";

                $('#phone_busy').attr('disabled','disabled');
                $('#callback').attr('disabled','disabled');
                $('#callok').attr('disabled','disabled');
                $('#phone_limited').attr('disabled','disabled');
                $('button[type="submit"]').removeAttr('disabled');
            },
            error: function() 
            {
                alert("Lỗi AJAX! Vui lòng liên hệ Admin!");
            }
        });
    });
    
     $( "#callok" ).on('click',function() {
        clearTimeout(refreshId);
        document.getElementById("phone_callstatus").value    = $(this).val();
        var phone       =   $("#phone").val();
        updateURL       = '/auth/sales/getphone?term=callok&phone='+phone;
        $.ajax(
        {
            url : updateURL,
            success:function(data)
            {
                document.getElementById("phone_busy").className      = "btn btn-success";
                document.getElementById("phone_limited").className   = "btn btn-success";
                document.getElementById("callback").className        = "btn btn-success";
                document.getElementById("callok").className          = "btn btn-danger";

                $('#phone_busy').attr('disabled','disabled');
                $('#callback').attr('disabled','disabled');
                $('#callok').attr('disabled','disabled');
                $('#phone_limited').attr('disabled','disabled');
                $('button[type="submit"]').removeAttr('disabled');
            },
            error: function() 
            {
                alert("Lỗi AJAX! Vui lòng liên hệ Admin!");
            }
        });
    });
    
    //send sms
    $(".sendsms").on('click',function(e){
            if($("#phone").val()==""){
                alert("SDT để gởi sms trống! Vui lòng kiểm tra lại!");return false;
            }
            setTimeout(function () {
                    $.ajax({
                        type        : "POST",
                        cache       : true,
                        url         : "/auth/telecom/saleSms",
                        data        : {phone: $("#phone").val()},
                        success: function(data) {
                                $.fancybox(data, {
                                        margin		: 0,
                                        padding		: 0,
                                        maxWidth	: '480px',
                                        maxHeight	: '400px',
                                        fitToView	: false,
                                        width		: '100%',
                                        height		: '100%',
                                        autoSize	: false,
                                        closeClick	: false,
                                        openEffect	: 'none',
                                        closeEffect	: 'none',
                                        titleShow	: true,
                                        closeBtn	: false,
                                }); // fancybox
                        }
                    });
                    e.preventDefault();	//STOP default action
            }, 1000);
    });
    
    wireUpEvents();
});

function wireUpEvents() {
    function goodbye(e){
        $.ajax(
            {
                url : '/auth/sales/updateStatus/',
                type: "POST",
                data : { phone: $("#phone").val(), status: 0}
            });
    }
    window.onbeforeunload=goodbye;
}
</script>